package com.neusoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.dao.DemandDao;
import com.neusoft.entity.Demand;
import com.neusoft.service.DemandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DemandServiceImpl implements DemandService {
    @Autowired
    private DemandDao demandDao;



    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Demand queryById(Integer id) {
        return this.demandDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public IPage<Demand> queryAllByLimit(int offset, int limit, Demand bean) {
        Page<Demand> page = new Page<>(offset,limit);
        QueryWrapper<Demand> wrapper = new QueryWrapper<>();
        wrapper.like("name",bean.getName());
        page.setRecords(demandDao.queryAll(page,bean));
        return page;
    }

    @Override
    public List<Demand> queryAll(Demand bean) {
        return null;
    }

    /**
     * 新增数据
     *
     * @param rent 实例对象
     * @return 实例对象
     */
    @Override
    public Demand insert(Demand rent) {
        this.demandDao.insert(rent);
        return rent;
    }

    /**
     * 修改数据
     *
     * @param rent 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Demand rent) {
        return this.demandDao.update(rent);
    }


    @Override
    public boolean deleteById(List<Integer> ids) {
        if(ids == null || ids.size()==0){
            return false;
        }
        StringBuffer sb = new StringBuffer("id in (");
        for(Integer item:ids){
            sb.append("'");
            sb.append(item);
            sb.append("',");
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append(")");

        return this.demandDao.delete("stu.demand",sb.toString()) > 0;
    }

    @Override
    public boolean deleteById1(Integer id) {
        if (id == null) {
            return false;
        }
        StringBuffer sb = new StringBuffer("id =");
        sb.append(id);
        return this.demandDao.delete1("stu.demand", sb.toString()) > 0;
    }
}
